using System; 
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine; 

namespace XFGameFramework.EquipmentSystem
{
    /// <summary>
    /// 装备配表
    /// </summary>
    [CreateAssetMenu(menuName ="XFKT/EquipmentSystem(装备配置)")]
    public class EquipmentsConfig : ScriptableObject
    {

        #region 字段

        /// <summary>
        /// 显示名
        /// </summary>
        [Tooltip("显示名")]
        public string displayName;

        [HideInInspector]
        public int start_id = 0;
        [Tooltip("装备配置集合")]
        [SerializeField]
        private List<EquipmentConfigData> equipments = new List<EquipmentConfigData>();

        private Dictionary<int,EquipmentConfigData> equipments_dic = new Dictionary<int, EquipmentConfigData>();
         
        public string excel_path;

        public string type_script_guid;

        #endregion

        #region 属性

        /// <summary>
        /// 装备配置集合
        /// </summary>
        public List<EquipmentConfigData> Equipments
        { 
            get
            {

#if UNITY_EDITOR

                if (equipments.Count == 0)
                {
                    string asset_path = AssetDatabase.GetAssetPath(this);
                    string guid = AssetDatabase.AssetPathToGUID(asset_path);
                    string path = string.Format("Assets/Extensions/EquipmentSystem/{0}", guid);

                    if (!AssetDatabase.IsValidFolder(path)) 
                    {
                        return equipments;
                    }

                    string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(EquipmentConfigData).FullName), new string[] { path });

                    foreach (var item in guids)
                    {
                        string p = AssetDatabase.GUIDToAssetPath(item);
                        EquipmentConfigData data = AssetDatabase.LoadAssetAtPath<EquipmentConfigData>(p);
                        if (data == null || equipments.Contains(data)) continue;
                        equipments.Add(data);
                    }


                    if (equipments.Count != 0)
                    {
                        EditorUtility.SetDirty(this);
                        AssetDatabase.SaveAssets();
                    }
                }

#endif

                return equipments;
            } 
        }

        #endregion

        /// <summary>
        /// 查询当前配表中的装备数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public EquipmentConfigData GetEquipmentConfigData(int id) 
        { 
            if(equipments_dic.Count == 0) 
            {
                foreach (var item in equipments)
                {

                    if(item == null) continue;

                    if (equipments_dic.ContainsKey(item.id))
                        throw new Exception(string.Format("装备id:{0}重复!",id));
                    equipments_dic.Add(item.id, item);
                }
            }

            if(equipments_dic.ContainsKey(id))
                return equipments_dic[id];
             
            return null;
        }
        
        /// <summary>
        /// 插件内部使用的
        /// </summary>
        public void ClearCache()
        { 
            equipments_dic.Clear();
        }

#if UNITY_EDITOR
        public Type GetDataType()
        {
            if (!string.IsNullOrEmpty(type_script_guid))
            {
                string asset_path = AssetDatabase.GUIDToAssetPath(type_script_guid);
                MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>(asset_path);
                if (script != null && script.GetClass() != null)
                {
                    Type type = script.GetClass();
                    if (type.IsSubclassOf(typeof(EquipmentConfigData)))
                        return type;
                }
            }

            return typeof(EquipmentConfigData);
        }

#endif

    }

}
